﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Soufun.DB;
using Soufun.Model;

namespace Soufun.DAL
{
    public class DUser
    {
        public int Add(UserInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Insert into tUser (");
            strSql.Append("UserName,Password,Permission,Remark)");
            strSql.Append(" value (");
            strSql.Append("@0,@1,@2,@4)");
            object[] paramaters = new object[]
                {
                    model.UserName,
                    model.Password,
                    model.Permission,
                    model.Remark 
                };
            int result = DbHelper.ExecuteSql(strSql.ToString(), paramaters);
            return result;
        }

        public int Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tUser where Id=");
            strSql.Append(id);
            int result = DbHelper.ExecuteSql(strSql.ToString());
            return result;
        }

        public UserInfo CheckUser(string userName, string password)
        {
            DataSet ds = GetList("UserName ='" + userName
                                 + "' and Password='" + password + "'");
            if (ds==null || ds.Tables.Count==0 || ds.Tables[0].Rows.Count==0)
            {
                return null;
            }
            UserInfo model = new UserInfo();
            if (ds.Tables[0].Rows[0]["UserName"].ToString()!="")
            {
                model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());             
            }
            model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
            model.Password = ds.Tables[0].Rows[0]["Password"].ToString();
            model.Permission = ds.Tables[0].Rows[0]["Permission "].ToString();
            model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
            return model;
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from tUser ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }            
            return DbHelper.Query(strSql.ToString());
        }
    }
}
